import router from '../../router'
import { useMenuStore } from '@/stores/menu'

router.beforeEach(async (to, from, next) => {
  const menuStore = useMenuStore()
  menuStore.setAside(to.meta.hasAside) // 设置侧边栏的状态
  menuStore.setMenuActive(to.path) // 设置当前激活的菜单
  document.title = `DyaoBook - ${to.meta.title}` // 设置页面标题

  if (menuStore.routes.length === 0) {
    await menuStore.setRoutes()
    let accessRoutes = menuStore.routes
    accessRoutes.forEach((item) => {
      router.addRoute(item)
    })
    next({ ...to, replace: true })
  } else {
    next()
  }
})

export function run() {
  console.log('router.js loaded')
}
